package test.yzx.pproject.act1;

import android.animation.Animator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;

import com.transitionseverywhere.TransitionManager;

import test.yzx.pproject.R;
import test.yzx.pproject.ViewAnimatorUtil;

public class Transition3Activity extends AppCompatActivity {

    private View block,text;
    private View bottomLayout;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.t3);
        block = findViewById(R.id.block);
        text = findViewById(R.id.text);
        bottomLayout = findViewById(R.id.bottomLayout);

        getWindow().getDecorView().postDelayed(new Runnable() {
            public void run() {
                goTransition();
                transitionBottom();
            }
        },1000);

    }


    private int duration = 800;

    private void goTransition(){
        com.transitionseverywhere.ChangeBounds cb = new com.transitionseverywhere.ChangeBounds();
        cb.setDuration(duration);
        cb.setInterpolator(new DecelerateInterpolator(0.8f));
        TransitionManager.beginDelayedTransition((ViewGroup) findViewById(R.id.container), cb);

        FrameLayout.LayoutParams block_lp = (FrameLayout.LayoutParams) block.getLayoutParams();
        block_lp.gravity = Gravity.TOP | Gravity.LEFT;
        block_lp.leftMargin = 80 * 3;
        block_lp.topMargin = 50 * 3;
        block_lp.width = block_lp.height = 90 * 3;

        FrameLayout.LayoutParams text_lp = (FrameLayout.LayoutParams) text.getLayoutParams();
        text_lp.gravity = Gravity.TOP | Gravity.LEFT;
        text_lp.leftMargin = 80 * 3 + 90 * 3 + 13 *3;
        text_lp.topMargin = (50 + 5) * 3;

        text.requestLayout();

        ViewAnimatorUtil.make(duration, 0, new DecelerateInterpolator(0.8f), new ViewAnimatorUtil.SimpleValueCallback(){
            public void onUpdate(Animator animation, float value) {
                text.setScaleX(value); text.setScaleY(value);
            }
        }, 1f, 0.7f).start();
    }


    private void transitionBottom(){
        final int height = bottomLayout.getMeasuredHeight();
        ViewAnimatorUtil.make(duration, 0, new DecelerateInterpolator(0.8f), new ViewAnimatorUtil.SimpleValueCallback(){
            public void onUpdate(Animator animation, float value) {
                bottomLayout.setTranslationY(value);
            }
        },height, 0).start();
    }


}
